package com.example.doctor.mapper;

import com.example.doctor.dto.req.GroupPatientsReq;
import com.example.doctor.dto.resp.GroupPatientsDb;


import com.example.doctor.dto.resp.PatientRsp;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;



import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Component
public interface GroupPatientsMapper {

    @Select("select pid_bigint from doctor_group_patients where gid_bigint = #{gid}")
    long[] GroupPatients(@Param("gid") Long gid);

    @Select("SELECT gid_bigint FROM doctor_group_main WHERE dc_id_bigint=#{dcId}")
    List<Long> getGidByDcId(@Param("dcId") Long dcId);


    @SelectProvider(type = GroupPatientsMapper.CommonProvider.class,method = "queryByDcId")
    List<GroupPatientsDb> queryByDcId(@Param("dcId") Long dcId,@Param("pids") List<Long> pids, @Param("search") String search);

    @Select("SELECT group_name_nvarchar AS groupName FROM doctor_group_main dg INNER JOIN doctor_group_patients dgp ON(dg.gid_bigint=dgp.gid_bigint) " +
            "WHERE dg.dc_id_bigint=#{dcId} AND dgp.pid_bigint=#{pid}")
    String queryByGroupName(@Param("pid") Long pid, @Param("dcId") Long dcId);

    @Select("SELECT header_nvarchar AS header,sex_nvarchar AS sex,birthday_date AS birthday, name_nvarchar AS name FROM client_patients_info WHERE pid_bigint =#{pid}")
    PatientRsp queryClientPatientsInfoByPid(@Param("pid") Long pid);

    @SelectProvider(type = GroupPatientsMapper.CommonProvider.class,method = "queryByGIDSQL")
    List<GroupPatientsDb> queryByGID(@Param("GID") Long GID, @Param("dcid") Long dcid, @Param("Search") String Search,@Param("skip")Integer skip,@Param("limit") Integer limit );
    @SelectProvider(type = GroupPatientsMapper.CommonProvider.class,method = "countByGIDSQL")
    Long countByGID(@Param("GID") Long GID, @Param("dcid") Long dcid, @Param("Search") String Search);

    @Select("select b.pid_bigint from doctor_group_main a"
    +" inner join doctor_group_patients b on a.gid_bigint = b.gid_bigint"
    +" where a.dc_id_bigint = #{dcid}")
    List<String> queryInGroupByDCID(@Param("dcid") Long dcid);

    @SelectProvider(type = GroupPatientsMapper.CommonProvider.class,method = "queryAllByGIDSQL")
    List<GroupPatientsDb> queryAllByGID(@Param("dcid") Long dcid, Boolean IsAll,@Param("gpid") String gpid, @Param("Search") String Search,@Param("skip") Integer skip ,@Param("limit") Integer limit);

    @SelectProvider(type = GroupPatientsMapper.CommonProvider.class,method = "countAllByGIDSQL")
    long countAllByGID(@Param("dcid") Long dcid, Boolean IsAll,@Param("gpid") String gpid, @Param("Search") String Search);
    class CommonProvider extends BaseProvider{

        public String GroupPatients(Map<String, Object> para){
            SQL sql = new SQL();
            sql.SELECT("  header_nvarchar AS header,sex_nvarchar AS sex,birthday_date AS birthday," +
                    "cpi.pid_bigint AS pid, name_nvarchar AS name, last_vi_st_time_datetime" +
                    " AS lastVistTime FROM doctor_group_patients dp INNER  JOIN rel_dr_and_patients rdp ON(dp.pid_bigint=rdp.pid_bigint) INNER JOIN client_patients_info  cpi ON (dp.pid_bigint=cpi.pid_bigint)");

            GroupPatientsReq groupPatientsReq = (GroupPatientsReq) para.get("groupPatientsReq");
            if (!ObjectUtils.isEmpty(groupPatientsReq.getSearch())){
                sql.WHERE("cpi.name_nvarchar LIKE CONCAT ('%',#{groupPatientsReq.Search},'%')");
            }
            filterFieldIdList(sql, "dp.gid_bigint", (List<Long>) para.get("gids"));
            return sql.toString();
        }

        public String queryByDcId(Map<String, Object> para){
            SQL sql = new SQL();
            sql.SELECT("header_nvarchar AS header,sex_nvarchar AS sex,birthday_date AS birthday,cpi.pid_bigint AS pid, " +
                    "name_nvarchar AS name ,last_vi_st_time_datetime AS lastVistTime FROM  rel_dr_and_patients rdp  " +
                    "INNER JOIN client_patients_info  cpi ON (rdp.pid_bigint=cpi.pid_bigint)");

            Long dcId = (Long) para.get("dcId");
            sql.WHERE("rdp.dc_id_bigint =#{dcId} and cpi.name_nvarchar LIKE CONCAT ('%',#{search},'%') ");
            List<Long> pids = (List<Long>) para.get("pids");
            if(pids.size() > 0)
                filterFieldNotIdList(sql, "cpi.pid_bigint", pids);
            return sql.toString();
        }
    
        public String queryByGIDSQL(@Param("GID") Long GID, @Param("dcid") Long dcid, @Param("Search") String Search,@Param("skip")Integer skip,@Param("limit") Integer limit){
            String sql = "select c.pid_bigint as pid,c.header_nvarchar as header,c.name_nvarchar as name,c.sex_nvarchar as sex,c.birthday_date as birthday,b.last_vi_st_time_datetime as lastVistTime"
            +" from doctor_group_patients a"
            +" inner join rel_dr_and_patients b on a.pid_bigint = b.pid_bigint"
            +" inner join client_patients_info c on b.pid_bigint = c.pid_bigint"
            +" where a.gid_bigint = #{GID} and b.dc_id_bigint = #{dcid} and c.disable_bit = 0 ";
            if (!Search.isEmpty() && Search.length() > 0)
                sql += " and c.name_nvarchar like concat('%',#{Search},'%') ";
            sql+=" order by b.last_vi_st_time_datetime desc limit #{skip},#{limit}";
            return sql;
        }

        public String countByGIDSQL(@Param("GID") Long GID, @Param("dcid") Long dcid, @Param("Search") String Search){
            String sql = "select count(*)"
                    +" from doctor_group_patients a"
                    +" inner join rel_dr_and_patients b on a.pid_bigint = b.pid_bigint"
                    +" inner join client_patients_info c on b.pid_bigint = c.pid_bigint"
                    +" where a.gid_bigint = #{GID} and b.dc_id_bigint = #{dcid} and c.disable_bit = 0";
            if (!Search.isEmpty() && Search.length() > 0)
                sql += " and c.name_nvarchar like concat('%',#{Search},'%')";
            return sql;
        }

        public String queryAllByGIDSQL(@Param("dcid") Long dcid, Boolean IsAll,@Param("gpid") String gpid, @Param("Search") String Search,@Param("skip") Integer skip ,@Param("limit") Integer limit){
            String sql = "with grou as ("
                +" select a.group_name_nvarchar,a.gid_bigint,b.pid_bigint,a.dc_id_bigint from doctor_group_main a"
                +" inner join doctor_group_patients b on a.gid_bigint = b.gid_bigint"
                +" where a.dc_id_bigint = #{dcid}"
                +" )select b.pid_bigint as pid,b.header_nvarchar as header,b.name_nvarchar as name,b.sex_nvarchar as sex,b.birthday_date as birthday,"
                +" a.last_vi_st_time_datetime as lastVistTime,ifnull(c.group_name_nvarchar,'无分组') as groupName, ifnull(c.gid_bigint,0) as Gid"
                +" from rel_dr_and_patients a "
                +" inner join client_patients_info b on a.pid_bigint = b.pid_bigint "
                +" left join grou c on a.dc_id_bigint = c.dc_id_bigint and a.pid_bigint = c.pid_bigint"
                +" where a.dc_id_bigint = #{dcid} and b.disable_bit = 0";
            if (!Search.isEmpty() && Search.length() > 0)
                sql += " and b.name_nvarchar like concat('%',#{Search},'%')";
            if(!IsAll)
                sql +=" and a.pid_bigint not in (${gpid})";
            sql+= " order by a.last_vi_st_time_datetime desc limit #{skip},#{limit}";
            return sql;
        }
        public String countAllByGIDSQL(@Param("dcid") Long dcid, Boolean IsAll,@Param("gpid") String gpid, @Param("Search") String Search){
            String sql = "with grou as ("
                    +" select a.group_name_nvarchar,a.gid_bigint,b.pid_bigint,a.dc_id_bigint from doctor_group_main a"
                    +" inner join doctor_group_patients b on a.gid_bigint = b.gid_bigint"
                    +" where a.dc_id_bigint = #{dcid}"
                    +" )select count(*)"
                    +" from rel_dr_and_patients a "
                    +" inner join client_patients_info b on a.pid_bigint = b.pid_bigint "
                    +" left join grou c on a.dc_id_bigint = c.dc_id_bigint and a.pid_bigint = c.pid_bigint"
                    +" where a.dc_id_bigint = #{dcid} and b.disable_bit = 0";
            if (!Search.isEmpty() && Search.length() > 0)
                sql += " and b.name_nvarchar like concat('%',#{Search},'%')";
            if(!IsAll)
                sql +=" and a.pid_bigint not in (${gpid})";
            return sql;
        }
    }
}
